package com.quasar.hpatient.module.home_bloodsugar;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.dialog.CalendarDialog;
import com.quasar.hpatient.dialog.InputDialog;
import com.quasar.hpatient.dialog.TagDialog;
import com.quasar.hpatient.dialog.TimeDialog;
import com.quasar.hpatient.dialog.WarningDialog;
import com.quasar.hpatient.module.home_daily.HomeDailyActivity;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.context.BaseApp;
import lib.quasar.context.BaseConstant;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.chart.ChartView;
import lib.quasar.widget.edit.AlterEditText;
import lib.quasar.widget.edit.CheckEditText;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 首页 ==> 血糖
 * created by kalu on 2018/3/29 9:03
 */
public class BloodsugarActivity extends BaseActivity<BloodsugarPresenter> implements BloodsugarView {

    public static final String MULIT = "MULIT";
    public static final String DATE = "DATE";

    @Override
    public void onBackPressed() {

        final int intExtra = getIntent().getIntExtra(HomeDailyActivity.POSITION, -1);
        final Intent result = getPresenter().getResult(this, intExtra);
        setResult(HomeDailyActivity.RESULT_CODE, result);
        super.onBackPressed();
    }

    @Override
    public int initView() {
        return R.layout.activity_home_bloodsugar;
    }

    @Override
    public void initDataLocal() {

        final boolean mulit = getIntent().getBooleanExtra(MULIT, false);

        final NavigationView menu = findViewById(R.id.home_bloodsugar_menu);
        menu.setMenuEnable(!mulit);
        if (mulit) {
            menu.setOnBackChangeListener(() -> onBackPressed());
        } else {
            menu.setOnNavigationChangeListener((isBack, isMenu) -> {
                if (isBack) onBackPressed();
                else if (isMenu) {
                    final RecyclerView recycler = findViewById(R.id.activity_home_bloodsugar_list);
                    final ChartView chart = findViewById(R.id.activity_home_bloodsugar_chart);
                    final EditText sugar = findViewById(R.id.activity_home_bloodsugar_input);
                    getPresenter().saveBloodsugar(this, chart, recycler, sugar);
                }
            });
        }

        final RecyclerView recycler = findViewById(R.id.activity_home_bloodsugar_list);
        getPresenter().initList(this, recycler, mulit);

        findViewById(R.id.activity_home_bloodsugar_date).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_bloodsugar_menu3).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_bloodsugar_menu4).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_bloodsugar_menu5).setVisibility(mulit ? View.GONE : View.VISIBLE);

        if (!mulit) {
            ((AlterEditText)findViewById(R.id.activity_home_bloodsugar_input)).setNumberFilter(1,3);
            findViewById(R.id.activity_home_bloodsugar_input).requestFocus();
            findViewById(R.id.activity_home_bloodsugar_time).setOnClickListener(v -> showTime());
            findViewById(R.id.activity_home_bloodsugar_tag).setOnClickListener(v -> showTag());
            TextView dates = findViewById(R.id.activity_home_bloodsugar_date);
            TextView times = findViewById(R.id.activity_home_bloodsugar_time);
            TextView tag = findViewById(R.id.activity_home_bloodsugar_tag);
            final String stringExtra = getIntent().getStringExtra(DATE);
            if (TextUtils.isEmpty(stringExtra)) {
                getPresenter().initTime(this, dates, times,tag);
            } else {
                setText(dates, stringExtra);
                getPresenter().initTime(this, null, times,tag);
            }
            dates.setOnClickListener(v -> {

                final String trim = dates.getText().toString().trim();
                try {
                    final int day = Integer.parseInt(trim.substring(trim.length() - 2, trim.length()));
                    showCalendar(dates, day);
                } catch (Exception e) {
                    LogUtil.e("", e.getMessage(), e);
                }
            });
        } else {
            TextView date = findViewById(R.id.activity_home_bloodsugar_date);
            final String stringExtra = getIntent().getStringExtra(DATE);
            setText(date, stringExtra);
        }
    }

    @Override
    public void initDataNet() {
        final TextView date = findViewById(R.id.activity_home_bloodsugar_date);
        final RecyclerView recycler = findViewById(R.id.activity_home_bloodsugar_list);
        final ChartView chart = findViewById(R.id.activity_home_bloodsugar_chart);
        final boolean mulit = getIntent().getBooleanExtra(MULIT, false);
        getPresenter().bloodsugarList(this, chart, recycler, date, mulit);
    }

    @Override
    public void clearInput() {

        EditText input1 = findViewById(R.id.activity_home_bloodsugar_input);
        if (null != input1) {
            input1.setText(BaseConstant.NULL_STR);
        }
    }

    @Override
    public void showDelete(final int position) {

        final WarningDialog dialog = new WarningDialog(this);
        dialog.show();
        dialog.setTitles("确定删除血糖");
        dialog.setButton("取消", "确定");
        dialog.setOnDialogChangeListener((left, right) -> {
            if (!right) return;
            final RecyclerView recycler = findViewById(R.id.activity_home_bloodsugar_list);
            final ChartView chart = findViewById(R.id.activity_home_bloodsugar_chart);
            getPresenter().deleteBloodsugar(BloodsugarActivity.this, chart, recycler, position);
        });
    }

    @Override
    public void showTime() {

        final TimeDialog dialog = new TimeDialog(this);
        dialog.show();
        dialog.setTitles("选择时间");
        dialog.setOnTimeChangeListener((hour, minute) -> {
            final TextView text = findViewById(R.id.activity_home_bloodsugar_time);
            TextView tag = findViewById(R.id.activity_home_bloodsugar_tag);
            getPresenter().setTag(this,tag,Integer.parseInt(hour),Integer.parseInt(minute));
            String date = BaseApp.getResource().getString(R.string.home_bloodsugar_times, hour, minute);
            text.setText(date);
        });
    }

    @Override
    public void showTag() {

        final TagDialog dialog = new TagDialog(this);
        dialog.show();
        dialog.setOnDialogChangeListener(str -> {
            TextView tag = findViewById(R.id.activity_home_bloodsugar_tag);
            setText(tag, str);
            TextView times = findViewById(R.id.activity_home_bloodsugar_time);
            getPresenter().setTime(this, times, str);
        });
    }

    @Override
    public void showCalendar(TextView text, int day) {

        final CalendarDialog dialog = new CalendarDialog(this);
        dialog.setScroll(getDate());
        dialog.show();
        dialog.setOnCalendarChangeListener((years, months, days, maxDay, isClick) -> {
            if (isClick) {
                final TextView date = findViewById(R.id.activity_home_bloodsugar_date);
                setText(date, years + "-" + months + "-" + days);
                final RecyclerView recycler = findViewById(R.id.activity_home_bloodsugar_list);
                final ChartView chart = findViewById(R.id.activity_home_bloodsugar_chart);
                final boolean mulit = getIntent().getBooleanExtra(MULIT, false);
                final TextView date1 = findViewById(R.id.activity_home_bloodsugar_date);
                getPresenter().bloodsugarList(this, chart, recycler, date, mulit);
            } else {
                getPresenter().schemelList(dialog, years, months, days, maxDay);
            }
        });
    }

    @Override
    public void showInput(String inputs, int position) {

        final InputDialog dialog = new InputDialog(this);
        dialog.show();
        dialog.setTities("修改血糖");
        dialog.setMenu(getResources().getString(R.string.home_temperature_menu));
        dialog.setMax(40f);
        dialog.setMin(1.7f);
//        dialog.setDotLength(1);
//        dialog.setMaxLength(4);
//        dialog.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
        dialog.setInputFilter(1,3);
        dialog.setHint(getResources().getString(R.string.home_bloodsugar_hint));
        dialog.setInput(inputs);
        dialog.setOnDialogChangeListener(input -> {
            final RecyclerView recycler = findViewById(R.id.activity_home_bloodsugar_list);
            final ChartView chart = findViewById(R.id.activity_home_bloodsugar_chart);
            getPresenter().modefyBloodsugar(this, chart, recycler, input, position);
        });
    }

    @Override
    public String getDate() {
        final TextView text = findViewById(R.id.activity_home_bloodsugar_date);
        return text.getText().toString();
    }

    @Override
    public String getTime() {
        final TextView text = findViewById(R.id.activity_home_bloodsugar_time);
        return text.getText().toString();
    }

    @Override
    public String getTag() {
        final TextView text = findViewById(R.id.activity_home_bloodsugar_tag);
        return text.getText().toString();
    }

    @Override
    public String getBloodsugar() {
        final TextView text = findViewById(R.id.activity_home_bloodsugar_input);
        return text.getText().toString();
    }
}